<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Event extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->model('product/product_model');
    }

    public function index(){
        if (count($_POST) > 0){
            /* Form validation */
            $this->load->library('form_validation');
            $this->form_validation->set_rules('fullname', 'Họ và tên', 'trim|required');
            $this->form_validation->set_rules('email', 'Địa chỉ Email', 'required|valid_email');
            $this->form_validation->set_rules('phone', 'Điện thoại', 'trim|required|min_length[10]');
            $this->form_validation->set_rules('identity_card', 'Số CMT', 'trim|required');
            $this->form_validation->set_rules('fb_nickname', 'Nickname facebook', 'trim|required|min_length[6]');
            $this->form_validation->set_rules('fb_url', 'Link Facebook', 'trim|required');
            $this->form_validation->set_rules('comment', 'Chia sẻ', 'trim|required|min_length[15]');

            if ($this->form_validation->run() == TRUE){
                /* Upload image */
                if ($_FILES['image']){
                    $upload_dir              = $_SERVER["DOCUMENT_ROOT"] . dirname($_SERVER['SCRIPT_NAME']) . '/uploads/event/1/';

                    /* Create dir if not exist */
                    if (!is_dir($upload_dir)){
                        @mkdir($upload_dir, 0755, TRUE);
                    }

                    /* Upload config */
                    $config['upload_path']   = $upload_dir;
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size']      = '800';
                    $config['max_width']     = '1800';
                    $config['max_height']    = '1200';

                    /* Library upload */
                    $this->load->library('upload', $config);

                    if (!$this->upload->do_upload('image')){
                        $data['status'] = FALSE;
                        $data['msg']    = $this->upload->display_errors();
                    }
                    else{
                        /* Image uploaded */
                        $image_data = $this->upload->data();

                        /* Rename file */
                        $upload_file = $image_data['full_path'];
                        $new_name = random_string('unique').$image_data['file_ext'];
                        @rename($upload_file, $upload_dir.$new_name);

                        $info = array(
                            'name' => 'Event 1',
                            'data' => json_encode(array(
                                'fullname'      => trim($this->input->post('fullname'), TRUE),
                                'email'         => trim($this->input->post('email'), TRUE),
                                'phone'         => trim($this->input->post('phone'), TRUE),
                                'identity_card' => trim($this->input->post('identity_card'), TRUE),
                                'fb_nickname'   => trim($this->input->post('fb_nickname'), TRUE),
                                'fb_url'        => trim($this->input->post('fb_url'), TRUE),
                                'gender'        => trim($this->input->post('gender'), TRUE),
                                'comment'       => trim($this->input->post('comment'), TRUE),
                                'image'         => $new_name
                            )),
                            'create_date' => date('Y-m-d H:i:s', time())
                        );
                        $result = $this->db->insert('event', $info);
                        if ($result){
                            $data['status'] = TRUE;
                            $data['msg']    = 'Cảm ơn bạn đã đăng ký thành công !';
                        }
                    }
                }
            } else {
                $data['status'] = FALSE;
            }
        }

        $data['total'] = $this->db->count_all_results('event');
        $data['tpl'] = 'home/event';
        $this->load->view('_layout/single', $data);
    }
}